package wowraidmanager.client.auction.data.calculate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;

import wowraidmanager.shared.model.auction.AuctionList;
import wowraidmanager.shared.model.auction.canvas.AuctionCalculateData;
import wowraidmanager.shared.model.auction.canvas.AuctionDistributionData;
import wowraidmanager.shared.model.auction.json.Auction;

public class CalculateData{

    public AuctionCalculateData calcAuctionData(AuctionList auctionList){
        AuctionCalculateData auctionCalculateData = new AuctionCalculateData();

        long minimum = Long.MAX_VALUE;
        long average = 0;
        int averagecount = 0;
        long maximum = Long.MIN_VALUE;
        for(Auction auction:auctionList.getAuctionData()){
            long buyout = Long.valueOf(auction.getBuyout());
            int quantity = Integer.valueOf(auction.getQuantity());

            if(buyout == 0){
                continue;
            }else{
                average += buyout;
                averagecount += quantity;

                long priceOne = buyout / quantity;
                if(minimum > priceOne){
                    minimum = priceOne;
                }

                if(maximum < priceOne){
                    maximum = priceOne;
                }
            }
        }

        average = average / averagecount;

        auctionCalculateData.setMaximumBuyoutPrice(maximum);
        auctionCalculateData.setMinimumBuyoutPrice(minimum);
        auctionCalculateData.setAverageBuyoutPrice(average);
        auctionCalculateData.setBuyoutQuantiry(averagecount);
        auctionCalculateData.setCalculateDate(auctionList.getDate());
        return auctionCalculateData;
    }

    @SuppressWarnings("unchecked")
    public ArrayList<AuctionCalculateData> calcAuctionDataList(ArrayList<AuctionList> auctionListList, Date dateFrom, Date dateTo){
        ArrayList<AuctionCalculateData> auctionCalculateData = new ArrayList<AuctionCalculateData>();

        if(auctionListList != null){
            for(AuctionList auctionList:auctionListList){
                Date getDate = auctionList.getDate();
                if(getDate.after(dateFrom) && getDate.before(dateTo)){
                    auctionCalculateData.add(calcAuctionData(auctionList));
                }
            }
            Collections.sort(auctionCalculateData, new DateComparator());
        }
        return auctionCalculateData;
    }

    public String toGold(long price){
        String goldString = "";
        String silverString = "";
        String copperString = "";

        long copper = price % 100;
        price /= 100;

        long silver = price % 100;
        price /= 100;

        if(price != 0){
            goldString = price + "G ";
        }

        if(silver != 0){
            silverString = silver + "S ";
        }

        if(copper != 0){
            copperString = copper + "C";
        }

        return goldString + silverString + copperString;
    }

    public ArrayList<AuctionDistributionData> calcAuctionDistributionData(AuctionList auctionList){
        ArrayList<AuctionDistributionData> auctionDistributionDataList = new ArrayList<AuctionDistributionData>();

        for(Auction auction:auctionList.getAuctionData()){
            if(auctionDistributionDataList.size() != 0){
                boolean isContain = false;
                long currentBuyoutPrice = Long.valueOf(auction.getBuyout()) / Long.valueOf(auction.getQuantity());
                for(AuctionDistributionData auctionDistributionData:auctionDistributionDataList){
                    if(currentBuyoutPrice == auctionDistributionData.getBuyoutPrice()){
                        auctionDistributionData.addTotalQuantity(Long.valueOf(auction.getQuantity()));
                        isContain = true;
                        break;
                    }
                }
                if(isContain == false){
                    auctionDistributionDataList.add(setDistributionData(auction));
                }
            }else{
                auctionDistributionDataList.add(setDistributionData(auction));
            }
        }

        Collections.sort(auctionDistributionDataList, new DistributionComparator());
        return auctionDistributionDataList;
    }

    private AuctionDistributionData setDistributionData(Auction auction){
        AuctionDistributionData auctionDistributionData = new AuctionDistributionData();
        long currentBuyoutPrice = Long.valueOf(auction.getBuyout()) / Long.valueOf(auction.getQuantity());
        auctionDistributionData.setBuyoutPrice(Long.valueOf(currentBuyoutPrice));
        auctionDistributionData.setTotalQuantity(Long.valueOf(auction.getQuantity()));
        return auctionDistributionData;
    }


    class DateComparator implements Comparator{
        public int compare(Object s, Object t){
            return ((AuctionCalculateData)s).getCalculateDate().compareTo(((AuctionCalculateData)t).getCalculateDate());
        }
    }

    class DistributionComparator implements Comparator{
        public int compare(Object s, Object t){
            return (int)(((AuctionDistributionData)s).getBuyoutPrice() - ((AuctionDistributionData)t).getBuyoutPrice());
        }

    }
}
